<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<meta http-equiv="X-UA-Compatible" content="ie=edge" />
		<title>underscore源码分析</title>
		<style>
			body {
				height: 3000px;
			}
			.last-item {
				color: red;
			}
		</style>
	</head>
	<body>
		<script src="js/underscore1.0.0.js"></script>
		<script>
			/* console.log(_.unique([1,2,3,4,5,4,5,6]))
    console.log(_.unique([1,2,3,4,5,4,5,6, 'a', 'A'], function(val) {
      return typeof val ==='string' ? val.toLocaleLowerCase() : val
    })) */
			// console.log(_([1,2,3,4,5,4,5,6, 'a', 'A']).chain().unique().map().value())

			// test(1, 2, 3, 4)
			function test(count, rest) {
				console.log(rest)
			}
			console.log(test.length)
			var restTest = _.restArguments(test)
			restTest(1, 2, 3, 4)

			console.log(
				_.reduce([1, 2, 3, 4, 5, 6], function(memo, value, index, obj) {
					return memo + value //1+2
				})
			)

			console.log(
				_.filter([12, 50, 2, 4, 6, 13, 12], function(elem) {
					return elem < 10
				})
			)
			console.log(_.indexOf([1, 2, 3, 4, 5, 6], 3, true))
			console.log(_.lastIndexOf([1, 2, 3, 4, 5, 6], 3, true))

			console.log(_.sample([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 5))
			var shuffleArray = _.shuffle([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
			console.log(shuffleArray)

			//深度展开
			console.log(_.flatten([1, [2, 3], [4, 5, [6]]]))
			// 浅度展开
			console.log(_.flatten([1, [2, 3], [4, [4]]], true))

			console.log(_.initial([1, 2, 3, 4, 5]))
			console.log(_.rest([1, 2, 3, 4, 5]))

			console.log(_.unique([1, 2, 3, 4, 5, 5, 6, 7, 7, 7, 8, 8, 9], true))

			console.log(_.range(5))
			console.log(_.range(1, 11))
			console.log(_.range(1, 11, 3))

			// 偏函数
			var add = function(a, b) {
				return a + b
			}

			var BindAdd = add.bind(this, 5)
			console.log(BindAdd(5))

			var partialAdd = _.partial(add, 5)
			console.log(partialAdd(10))

			// cache函数
			var hasher = function() {
				var n = arguments[0]
				return n + 'abc'
			}

			var fibonacci = _.memoize(function(n) {
				return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2)
			}, hasher)
			console.log(fibonacci(100))
			console.log(fibonacci.cache)

			_.delay(
				function(name, age) {
					console.log(name, age)
				},
				1500,
				'max',
				30
			)

			function A(data) {
				console.log('A')
				return data * 2
			}
			function B(data) {
				console.log('B')
				return data + 2
			}
			function C(data) {
				console.log('C')
				return data - 2
			}
			// 函数组合
			var ret = _.compose(
				A,
				B,
				C
			) // 依次调用 上一次执行函数    返回值传给下一个要执行的函数
			console.log(ret(4))

			// 节流防抖
			// 没有配置时执行两次， 立即执行一次， 间隔 1500毫秒后再执行一次
			var throttle = _.throttle(
				function() {
					console.log('hello throttle')
				},
				1500,
				{
					// leading: false // 阻止立即调用 1500毫秒后执行一次
					trailing: false, // 立即调用，不用等待
				}
			)
			// window.onscroll = throttle
			var debounce = _.debounce(function() {
				console.log('hello debounce')
			}, 1500)
			window.onscroll = debounce

			_.pick(
				{
					name: 'max',
					age: 30,
					userid: 'wzy',
				},
				function(value, key, object) {
					return _.isNumber(value)
				}
			)
		</script>
	</body>
</html>
